//
//  THEarningBrowserVC.m
//  YJB
//
//  Created by YJB on 2016/11/26.
//  Copyright © 2016年 YJB. All rights reserved.
//  正在赚钱

#import "THEarningBrowserVC.h"
#import "YJUserDefaultTool.h"
#import "YJNetworkTool.h"
#import "NJKWebViewProgress.h"
#import "NJKWebViewProgressView.h"
#import "masonry.h"
#import "YJPopMenuView.h"
#import "THFlowEarnMyPublishVC.h"
#import "THFlowEarnCurrentTaskDetailVC.h"

@interface THEarningBrowserVC ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
@property(nonatomic,strong)UIWebView  *webView;
/** 进度条代理者 */
@property(nonatomic,strong)NJKWebViewProgress *progressProxy;
/** 进度条 */
@property(nonatomic,strong)NJKWebViewProgressView *progressView;
@end

@implementation THEarningBrowserVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"正在赚钱";
    UIBarButtonItem *choiceBtn = [UIBarButtonItem itemWithTarget:self action:@selector(moreChoiceBtn:) image:@"展开" highImage:nil width:25 imageOffset:8];
    self.navigationItem.rightBarButtonItem = choiceBtn;
    [self loadData];
}

- (void)moreChoiceBtn:(UIButton *)button
{
    NSArray *titles = @[@"我发布的",@"任务详情"];
    NSArray *images = @[@"我发布的",@"任务详情"];
    YJPopMenuView *popView = [YJPopMenuView popMenuViewWithTitles:titles images:images];
    popView.cellClickBlock = ^(NSIndexPath *indexPath){
        [self menuCellClick:indexPath];
    };
    [popView showFrom:CGPointMake(button.x + button.width * 0.5, 64)];
}

- (void)menuCellClick:(NSIndexPath *)indexPath
{
    switch (indexPath.row)
    {
        case 0://我发布的
        {
            THFlowEarnMyPublishVC *vc = [[THFlowEarnMyPublishVC alloc] init];
            vc.navTitle = @"我发布的";
            vc.isAuto = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1://任务详情"
        {
            THFlowEarnCurrentTaskDetailVC *vc = [[THFlowEarnCurrentTaskDetailVC alloc] init];
            vc.title = @"任务详情";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
    }
}

- (void)loadData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"action"] = @"get_traff_url";
    params[@"makegold"] = @{@"username":[YJUserDefaultTool readUserName],
                            @"user_info":[YJUserDefaultTool readUserName],
                            @"phone_info":@"iOS",
                            @"md5_key":[YJUserDefaultTool readKeyID]};
    [MBProgressHUD showMessage:@"" toView:self.view];
    [YJNetworkTool postWithUrl:THURL(@"traffic.php") parameters:params success:^(id responseObject) {
        MyLog(@" ==== %@",responseObject);
        NSString *code = responseObject[@"return"][@"code"];
        [MBProgressHUD hideHUDForView:self.view];
        if (code.integerValue == 200)
        {
            NSString *urlString = responseObject[@"return"][@"data"];
            if (![urlString hasPrefix:@"http"])
                urlString = [@"http://" stringByAppendingString:urlString];
            
            NSURL *url = [NSURL URLWithString:urlString];
            NSURLRequest *req = [NSURLRequest requestWithURL:url];
            [self.webView loadRequest:req];
        }else
        {
            NSString *message = responseObject[@"return"][@"message"];
            [MBProgressHUD showError:message toView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
    }];
}

- (UIWebView *)webView
{
    if (_webView == nil)
    {
        _webView = [[UIWebView alloc]init];
        _progressProxy = [[NJKWebViewProgress alloc] init];
        _webView.delegate = _progressProxy;
        _progressProxy.webViewProxyDelegate = self;
        _progressProxy.progressDelegate = self;
        [self.view addSubview:_webView];
        _webView.backgroundColor = WHITECOLOR;
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(0);
        }];
    }
    return _webView;
}

- (NJKWebViewProgressView *)progressView
{
    if (_progressView == nil)
    {
        _progressView = [[NJKWebViewProgressView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_W, 3)];
        _progressView.progressBarView.backgroundColor = APPCOLOR;
        [self.view addSubview:_progressView];
        [self.view bringSubviewToFront:_progressView];
        
    }
    return _progressView;
}


#pragma ------------------------ 进度条代理
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [self.progressView setProgress:progress animated:YES];
}


@end
